<template>
  <view class="app-content">
    <view class="logo-content" @click="dblclick">
      <image style="width: 200rpx;height: 100rpx;" src="../../static/WechatIMG.png" mode="widthFix" />
      <!-- <text class="title">管理系统登录</text> -->
    </view>
    <view class="login-form-content">
      <view class="input-item">
        <view class="iconfont icon-user" />
        <input v-model="username" class="input" type="text" placeholder="请输入账号" maxlength="30">
      </view>
      <view class="input-item">
        <view class="iconfont icon-password" />
        <input v-model="password" :type="showPassword ? 'text' : 'password'" class="password_input" placeholder="请输入密码" maxlength="20">
        <u-icon class="iconfont" style="margin-left: 0px;" :name="showPassword ? 'eye-fill' : 'eye'" size="30" @click="togglePasswordVisibility" />
      </view>
      <view class="action-btn">
        <button class="login-btn" type="primary" @click="bindLogin">登录</button>
      </view>
      <!-- <view v-if="register" class="reg text-center">
        <text class="text-grey1">没有账号？</text>
        <text class="text-blue" @click="handleUserRegister">立即注册</text>
      </view> -->
    </view>

    <u-modal
      v-model="adminShow"
      title="请输入管理员密码"
      :show-cancel-button="true"
      :title-style="{ color: 'red' }"
      @confirm="adminConfirm"
    >
      <u-input v-model="adminPassword" class="modal-btn" height="80" :border="true" type="password" placeholder="请输入管理员密码" />
    </u-modal>

    <u-modal
      v-model="baseUrlShow"
      title="请选择接入服务"
      :show-cancel-button="true"
      :title-style="{ color: 'red' }"
      @confirm="baseUrlConfirm"
    >
      <u-button class="modal-btn" type="success" @click="baseURL = '124.221.253.237:88'"> 记账管理 </u-button>
      <u-button class="modal-btn" type="success" @click="baseURL = '124.221.253.237:99'"> 任务管理 </u-button>
      <u-button class="modal-btn" type="success" @click="baseURL = '124.221.253.237:80'"> 投资管理 </u-button>
      <u-input v-model="baseURL" class="modal-btn" height="80" :border="true" type="text" placeholder="支持自定义,格式 ip:port" />
    </u-modal>

    <!-- 无网络监测 -->
    <u-no-network />
  </view>
</template>

<script>
export default {
  data() {
    return {
      username: 'admin',
      password: 'admin',
      positionTop: 0,
      isDevtools: false,
      warningShow: false,
      adminShow: false,
      adminPassword: '',
      baseUrlShow: false,
      showPassword: false,
      baseURL: ''
    }
  },

  onShow() {
    // if (!uni.getStorageSync('baseURL')) {
    //   uni.setStorageSync('baseURL', '124.221.253.237:80')
    // }
    const platform = uni.getSystemInfoSync().platform
    uni.setStorageSync('platform', platform)
  },

  methods: {
    initPosition() {
      // 使用 absolute 定位，并且设置 bottom 值进行定位。软键盘弹出时，底部会因为窗口变化而被顶上来。
      // 反向使用 top 进行定位，可以避免此问题。
      this.positionTop = uni.getSystemInfoSync().windowHeight - 100
    },

    adminConfirm() {
      if (this.adminPassword === '253547') {
        this.baseUrlShow = true
      } else {
        this.$u.toast('管理员密码不正确!')
      }
    },

    baseUrlConfirm() {
      uni.setStorageSync('baseURL', this.baseURL)
      setTimeout(() => {
        plus.runtime.restart()
      }, 500)
    },

    dblclick(e) {
      var that = this
      var currentTime = e.timeStamp
      var lastTapTime = that.lastTapTime
      that.lastTapTime = currentTime
      if (currentTime - lastTapTime < 300) {
        this.adminPassword = ''
        this.adminShow = true
        clearTimeout(that.lastTapTimeoutFunc)
        console.log('双击事件已触发')
        this.createLocalNotification()
      }
    },

    bindLogin() {
      if (this.username.length === 0) {
        uni.showToast({
          icon: 'none',
          title: '账号不能为空'
        })
        return
      }
      if (this.password.length === 0) {
        uni.showToast({
          icon: 'none',
          title: '账号不能为空'
        })
        return
      }
      // 登录请求 成功后保存token
      this.$u.api
        .login({
          username: this.username,
          password: this.password
        })
        .then(res => {
          if (res.code === 200) {
            uni.setStorageSync('token', res.data.pageItems)
            uni.setStorageSync('username', this.username)
            this.$u.toast('登录成功!')
            uni.switchTab({
              url: '/pages/home/index'
            })
          }
        })
    },

    togglePasswordVisibility() {
      this.showPassword = !this.showPassword
    }
  },

  onReady() {
    this.initPosition()
  }
}
</script>

<style lang="less" scoped>
@import './index';
</style>
